#include "Model.h"
#include "Enum.h"
#define DIRECTINPUT_VERSION 0x0800
#include <dinput.h>

Model::Model()
{
	state = MENU;
	newState = true;
}

void Model::changeState(int a_state)
{
	switch(state)
	{
	case MENU:
		{
			menu.Close();
			SoundStruct music;
			music.Music = GAME_MUSIC;
			playList.add(music);
		}
		break;
	case GAME:	game.Close();
	case SYNCH:	synch.End();	break;
	};
	state = a_state;
	newState = true;
	switch(state)
	{
	case MENU:	menu.Open();			break;
	case GAME:
		{
			game.Open();
			synch.Start(SERVER);
		}
		break;
	case SYNCH:	synch.Start(CLIENT);	break;
	};
}

//Server update loop
	//synch
	//update
//Client update loop
	//synch

bool Model::Update(InputStruct P1_input)
{
	switch(state)
	{
	case MENU:
		{
			return menu.Update(P1_input.mousePos, P1_input.mouseClicked);
		}
		break;
	case GAME:
		{
			InputStruct P2_input = synch.Synchronize(&drawList);
			game.Update(P1_input, P2_input);
			//if(game.GameOver())
				//return false;
		}
		break;
	case SYNCH:
		{
			DrawListMessage* DrLiMe = synch.Synchronize(P1_input);
			if(DrLiMe)
			{
				drawList.clear();
				playList.clear();

				LostVector<DrawStruct>* draw = DrLiMe->getDrawList();
				for(int i=0;i<draw->Size();++i)
					drawList.add(draw->get(i));
			}
		}
		break;
	};
	return true;
}

void Model::Draw(LostVector<DrawStruct>* draw, LostVector<SoundStruct>* sound)
{
	DrawStruct drawSt;
	SoundStruct soundSt;
	switch(state)
	{
	case MENU:
		{
			if(newState)
			{
				menu.SetMusic();
			}
			menu.Render(draw, sound);
		}
		break;
	case GAME:
		{
			drawList.clear();
			game.PushIntoDrawStruct(&drawList);

			for(int i=0;i<drawList.Size();++i)
				draw->add(drawList.get(i));
			for(int i=0;i<playList.Size();++i)
				sound->add(playList.get(i));

			playList.clear();
		}
		break;
	case SYNCH:
		{
			for(int i=0;i<drawList.Size();++i)
				draw->add(drawList.get(i));
			for(int i=0;i<playList.Size();++i)
				sound->add(playList.get(i));
		}
		break;
	};
}